/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.interfacce;

import Bean.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mimmo
 */
public class UserDAO_DB implements UserDAO {

    Connection connection;

    public UserDAO_DB(String connectionString, String username, String password) throws Exception {
        connection = DriverManager.getConnection(connectionString, username, password);
    }

    public User getWorker(String username) {
        Statement s = null;
        User utente = null;
        try {
            s = connection.createStatement();
            ResultSet r = s.executeQuery("SELECT * FROM user where username= '"+ username + "' ");
            while (r.next()) {
                    utente=new User();
                    utente.setUsername(username);
                    utente.setPassword(r.getString("password"));
                    utente.setWork(r.getString("work"));
                    utente.setName(r.getString("name"));
                    utente.setSurname(r.getString("surname"));
                    utente.setBirthDate(r.getString("birthDate"));
                
                break;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO_DB.class.getName()).log(Level.SEVERE, null, ex);
        }


        return utente;

    }

    public boolean isWorker(String username) {
        Statement s = null;
        boolean exist = false;
        try {
            s = connection.createStatement();
            ResultSet r = s.executeQuery("SELECT * from user");
            while (r.next()) {
                if (r.getString("username").equalsIgnoreCase(username)) {
                    exist= true;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO_DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return exist;
    }

    

    public void storeWorker(String username, String password, String work, String name, String surname, String birthDate) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
